package com.xinyue.dz.dao.impl;

import com.xinyue.dz.bean.UserRole;
import com.xinyue.dz.dao.UserRoleDao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xinyue.dz.bean.User;
import com.xinyue.dz.bean.Role;

@Repository
public class UserRoleDaoImpl implements UserRoleDao {
	@Autowired
    private SessionFactory factory;
    
	//默认方法
	@Override
	public void save(UserRole entity) {
		factory.getCurrentSession().save(entity);
	}
	
	@Override
	public Integer deleteById(int id) {
		Query q = factory.getCurrentSession().createQuery("delete UserRole where id=?");
		q.setParameter(0, id);
		return q.executeUpdate();
	}
	
	@Override
	public Long queryRoleCountByUserId(int userId) {
		Query q = factory.getCurrentSession().createQuery("select count(*) from UserRole a,Role o where o.roleId=a.roleId and a.userId=:userId");
		q.setInteger("userId", userId);
		return (Long) q.uniqueResult();
	};
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Role> queryRoleByUserId(int userId) {
		Query q = factory.getCurrentSession().createQuery("select o from UserRole a,Role o where o.roleId=a.roleId and a.userId=:userId");
		q.setInteger("userId", userId);
		return q.list();
	};
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Role> queryRoleByUserId(int userId, int begin, int count) {
		Query q = factory.getCurrentSession().createQuery("select o from UserRole a,Role o where o.roleId=a.roleId and a.userId=:userId");
		q.setInteger("userId", userId);
		q.setFirstResult(begin);
		q.setMaxResults(count);
		return q.list();
	};
	
	@Override
	public Long queryUserCountByRoleId(int roleId) {
		Query q = factory.getCurrentSession().createQuery("select count(*) from UserRole a,User o where o.userId=a.userId and a.roleId=:roleId");
		q.setInteger("roleId", roleId);
		return (Long) q.uniqueResult();
	};
	
	@Override
	@SuppressWarnings("unchecked")
	public List<User> queryUserByRoleId(int roleId) {
		Query q = factory.getCurrentSession().createQuery("select o from UserRole a,User o where o.userId=a.userId and a.roleId=:roleId");
		q.setInteger("roleId", roleId);
		return q.list();
	};
	
	@Override
	@SuppressWarnings("unchecked")
	public List<User> queryUserByRoleId(int roleId, int begin, int count) {
		Query q = factory.getCurrentSession().createQuery("select o from UserRole a,User o where o.userId=a.userId and a.roleId=:roleId");
		q.setInteger("roleId", roleId);
		q.setFirstResult(begin);
		q.setMaxResults(count);
		return q.list();
	};
	
	@Override
	public Integer deleteByUserId(int userId) {
		Query q = factory.getCurrentSession().createQuery("delete UserRole where userId=?");
		q.setInteger("userId", userId);
		return (Integer) q.uniqueResult();
	};
	
	@Override
	public Integer deleteByRoleId(int roleId) {
		Query q = factory.getCurrentSession().createQuery("delete UserRole where roleId=?");
		q.setInteger("roleId", roleId);
		return (Integer) q.uniqueResult();
	};
}